﻿<template>
  <v-card>
    <v-card-title>
      <span class="headline" v-text="isEdit ? '修改员工' : '新增员工'"></span>
    </v-card-title>
    <v-divider/>
    <v-card-text class="px-3">
      <v-form v-model="valid" ref="myMemberForm">
        <v-container grid-list-md>
          <v-layout wrap>
            <v-flex class="md6">
              <v-text-field v-model="member.username" label="员工姓名" required />
            </v-flex>
            <v-flex class="md6">
              <v-text-field v-model="member.phone" label="联系方式" required />
            </v-flex>
            <v-flex class="md6">
              <v-text-field v-model="member.password" label="员工密码" required />
            </v-flex>
            <v-flex>
              <v-cascader
                      itemText="roleName"
                      itemValue="id"
                      url="/uman/queryRole"
                      multiple
                      required
                      v-model="member.roles"
                      label="员工角色"/>
            </v-flex>
          </v-layout>
        </v-container>
      </v-form>
    </v-card-text>
    <v-card-actions>
      <v-spacer/>
      <v-btn depressed  small @click="submit">提交</v-btn>
      <v-btn depressed small @click="clear">重置</v-btn>
      <v-btn depressed  small @click="close">关闭</v-btn>
    </v-card-actions>
  </v-card>

</template>

<script>
  export default {
    name: "member-form",
    props: {
      oldMember: {
        type: Object
      },
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        valid: false, // 表单校验结果标记
        member: {
          username: '', // 员工姓名
          phone: '', // 联系方式
          password: '', // 员工密码
          roles: {}, // 员工职位
        },
      }
    },
    methods: {
      submit() {
        // 表单校验
        if (this.$refs.myMemberForm.validate()) {
          // 定义一个请求参数对象，通过解构表达式来获取member中的属性
          const { ...params} = this.member;
          // 将数据提交到后台
          // this.$http.post('/auth/member', this.$qs.stringify(params))
          this.$http({
            method: this.isEdit ? 'put' : 'post',
            url: '/uman/member',
            data: params
          }).then(() => {
            // 关闭窗口
            this.$emit("close");
            this.$message.success("保存成功！");
          })
            .catch(() => {
              this.$message.error("保存失败！");
            });
        }
      },
      clear() {
        // 重置表单
        this.$refs.myMemberForm.reset();
        // 需要手动清空员工职位
        this.member.roles = {};
      },
      close(){
        this.$emit("close");
      }
    },
    watch: {
      oldMember: {// 监控oldMember的变化
        handler(val) {
          if (val) {
            // 注意不要直接复制，否则这边的修改会影响到父组件的数据，copy属性即可
            this.member = Object.deepCopy(val)
          } else {
            // 为空，初始化member
            this.member = {
              username: '',
              phone: '',
              password: '',
              roles: {},
            };
            this.$refs.myMemberForm.reset();
          }
        },
        deep: true
      }
    }
  }
</script>

<style scoped>

</style>
